import Cookies from 'js-cookie'
import Layout from '@/layout'
// import LayoutHome from '@/layoutHome'
const TokenKey = 'Admin-Token'
import { forEach } from '@/libs/tools'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}
export function getMenuByRouter(list) {
  const res = []
  forEach(list, item => {
    if (!item.meta || (item.meta && !item.meta.hideInMenu)) {
      const obj = {
        icon: (item.meta && item.meta.icon) || '',
        name: item.name,
        meta: item.meta
      }
      if ((hasChild(item) || (item.meta && item.meta.showAlways))) {
        obj.children = getMenuByRouter(item.children)
      }
      if (item.meta && item.meta.href) obj.href = item.meta.href
      res.push(obj)
    }
  })
  return res
}

export function getResFunctions(list, resId) {
  var codeArray = []
  list.forEach((item) => {
    if (item.resId === resId) {
      if (item.functionCodes && item.functionCodes.length > 0) {
        codeArray = item.functionCodes.split(',')
      }
      return codeArray
    }
  })
  return codeArray
}

export function initRouterNode(routers, menus, resFunctionList, flag) {
  if (menus) {
    menus.forEach((item) => {
      const menu = {}
      var meta = {
        icon: item.resIcon ? item.resIcon : 'ios-book',
        title: item.resName ? item.resName : '未定义',
        resFunctions: getResFunctions(resFunctionList, item.resId)
      }
      if (item.resCode === 'mh') {
        meta.affix = true
      }
      if (item.children && item.children.length > 0) {
        menu.children = []
        initRouterNode(menu.children, item.children, resFunctionList, false)
        if (menu.children.length === 1) {
          meta.hideInBread = true
        }
        if (item.resUrl && item.resUrl.length > 0) {
          menu.component = (resolve) => require([`@/views${item.resUrl}.vue`], resolve)
        } else {
          if (flag) {
            // if (item.resCode === 'homePage') {
            //   menu.component = LayoutHome
            // } else {
            //   menu.component = Layout
            // }
            menu.component = Layout
          } else {
            menu.component = () => import('@/views/blank.vue')
          }
        }
      } else {
        if (flag) {
          menu.children = []
          initRouterNode(item, item.children, resFunctionList, false)
          if (menu.children.length === 1) {
            meta.hideInBread = true
          }
        }
        if (item.resUrl && item.resUrl.length > 0) {
          menu.component = (resolve) => require([`@/views${item.resUrl}.vue`], resolve)
        } else {
          // if (item.resCode === 'mh' || item.resCode === 'decisionCenter') {
          //   menu.component = LayoutHome
          // } else {
          //   menu.component = Layout
          // }

          menu.component = Layout
        }
      }
      menu.meta = meta
      menu.path = item.resUrl ? item.resUrl : '/'
      menu.name = item.resCode
      menu.icon = item.resIcon ? item.resIcon : 'ios-book'
      /* if (item.resUrl && item.resUrl.length > 0) {
        menu.component = () => import('@/view' + item.resUrl + '.vue')
      } else {
        menu.component = Main
      } */
      routers.push(menu)
    })
  }
}

export function hasChild(item) {
  return item.children && item.children.length !== 0
}
